package com.fgs.common.progress;

import android.net.ParseException;

import com.google.gson.JsonParseException;

import org.json.JSONException;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
/**
 * @author fgs
 * @date 2020/3/27
 **/

/**
 * 错误处理驱动
 */
public class ExceptionHandle {

    private static final int UNAUTHORIZED = 401;
    private static final int FORBIDDEN = 403;
    private static final int NOT_FOUND = 404;
    private static final int REQUEST_TIMEOUT = 408;
    private static final int INTERNAL_SERVER_ERROR = 500;
    private static final int BAD_GATEWAY = 502;
    private static final int SERVICE_UNAVAILABLE = 503;
    private static final int GATEWAY_TIMEOUT = 504;

    public static ResponseThrowable handleException(Throwable e) {
        ResponseThrowable ex;
        if (e instanceof UnknownHostException) {
            ex = new ResponseThrowable(e, ERROR.NO_NET_WORK);
            ex.message = "请打开网络";
            return ex;
        } else if (e instanceof ServerException) {
            ServerException resultException = (ServerException) e;
            ex = new ResponseThrowable(resultException, resultException.code);
            ex.message = resultException.message;
            return ex;
        } else if (e instanceof JsonParseException
                || e instanceof JSONException
                || e instanceof ParseException) {
            ex = new ResponseThrowable(e, ERROR.PARSE_ERROR);
            ex.message = "解析错误";
            return ex;
        } else if (e instanceof ConnectException) {
            ex = new ResponseThrowable(e, ERROR.NETWORK_ERROR);
            ex.message = "网络连接失败，请检查网络设置";//网络连接失败
            return ex;
        } else if (e instanceof javax.net.ssl.SSLHandshakeException) {
            ex = new ResponseThrowable(e, ERROR.SSL_ERROR);
            ex.message = "证书验证失败";
            return ex;
        } else if (e instanceof SocketTimeoutException) {
            ex = new ResponseThrowable(e, REQUEST_TIMEOUT);
            ex.message = "请求网络超时";
            return ex;
        } else if (e instanceof com.jakewharton.retrofit2.adapter.rxjava2.HttpException) {
            return convertStatusCode(e);
        } else {
            ex = new ResponseThrowable(e, ERROR.UNKNOWN);
//            ex.message = "未知错误";
            e.printStackTrace();
            ex.message = "未知错误：\n" + e.getMessage();
            return ex;
        }

    }

    private static ResponseThrowable convertStatusCode(Throwable e) {
        com.jakewharton.retrofit2.adapter.rxjava2.HttpException httpException = (com.jakewharton.retrofit2.adapter.rxjava2.HttpException) e;
        ResponseThrowable ex;
        switch (httpException.code()) {
            case UNAUTHORIZED:
                ex = new ResponseThrowable(e, UNAUTHORIZED);
//                ex.message = "Token已过期";
                ex.message = "";
                return ex;
            case FORBIDDEN:
                ex = new ResponseThrowable(e, FORBIDDEN);
                ex.message = "请求被服务器拒绝";
                return ex;
            case NOT_FOUND:
                ex = new ResponseThrowable(e, NOT_FOUND);
                ex.message = "请求地址不存在";
                return ex;
            case REQUEST_TIMEOUT:

            case GATEWAY_TIMEOUT:
                ex = new ResponseThrowable(e, REQUEST_TIMEOUT);
                ex.message = "请求网络超时";
                return ex;
            case INTERNAL_SERVER_ERROR:
                ex = new ResponseThrowable(e, INTERNAL_SERVER_ERROR);
                ex.message = "服务器发生错误";
                return ex;
//            case BAD_GATEWAY:
//            case SERVICE_UNAVAILABLE:
            default:
                ex = new ResponseThrowable(e, ERROR.UNKNOWN);
//                ex.message = "未知错误";
                ex.message = "未知错误：\n" + e.getMessage();
                return ex;
        }

    }

    /**
     * 约定异常
     */
    public class ERROR {
        /**
         * 未知错误
         */
        public static final int UNKNOWN = 1000;
        /**
         * 解析错误
         */
        public static final int PARSE_ERROR = 1001;
        /**
         * 网络错误
         */
        public static final int NETWORK_ERROR = 1002;
        /**
         * 协议出错
         */
        public static final int HTTP_ERROR = 1003;

        /**
         * 证书出错
         */
        public static final int SSL_ERROR = 1005;
        /**
         * 没有网络
         */
        private static final int NO_NET_WORK = 1006;
    }

    public static class ResponseThrowable extends Exception {
        public int code;
        public String message;

        public ResponseThrowable(Throwable throwable, int code) {
            super(throwable);
            this.code = code;
        }
    }

    public class ServerException extends RuntimeException {
        public int code;
        public String message;
    }
}
